﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBA.WindowsServices
{
    public abstract class WindowsServiceOutput : IWindowsServiceOutput
    {

        public WindowsServiceOutput(IWindowsServiceOutput remote)
        {
            this.RemoteOutput = remote;
        }

        public IWindowsServiceOutput RemoteOutput { get; set; }

        public void Write(OutputLevel level, string format, params object[] args)
        {
            this.Write(level, string.Format(format, args));
        }

        public void WriteLine(OutputLevel level, string format, params object[] args)
        {
            this.Write(level, string.Format(format, args) + Environment.NewLine);
        }

        protected virtual void Write(OutputLevel level, string message)
        {
            switch (level)
            {
                case OutputLevel.Trace:
                    this.WriteTrace(message);
                    break;
                case OutputLevel.Debug:
                    this.WriteDebug(message);
                    break;
                case OutputLevel.Info:
                    this.WriteInfo(message);
                    break;
                case OutputLevel.Warn:
                    this.WriteWarn(message);
                   
                    break;
                case OutputLevel.Error:
                    this.WriteError(message);
     
                    break;
                case OutputLevel.Fatal:
                    this.WriteFatal(message);
        
                    break;
                default:
                    break;
            }

            try
            {
                if (this.RemoteOutput != null)
                {
                    this.RemoteOutput.Write(level, message);
                }
            }
            catch (Exception ex)
            {
                this.RemoteOutput = null;
                this.WriteError(ex.ToString());
            }
        }

        protected abstract void WriteFatal(string message);

        protected abstract void WriteError(string message);

        protected abstract void WriteWarn(string message);

        protected abstract void WriteInfo(string message);

        protected abstract void WriteDebug(string message);

        protected abstract void WriteTrace(string message);


    }
}
